//
// Copyright (c) 2003, 2004 Robert Mooney <rjmooney@impetus.us>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

using System;
using System.IO;
using System.Threading;

namespace MooneyTool
{
    public class Logger
    {
        private static Log instance;

        private Logger()
        {

        }

        public static Log getInstance()
        {
            if(instance == null)
            {
                instance = new Log(Directory.GetCurrentDirectory() + "/log.txt");
            }

            return instance;
        }
    }

	/// <summary>
	/// A simple syslog-style flat-file logging class.
	/// </summary>
	public class Log
	{
		protected Mutex m_mutex;					// The mutex to synchronize (and protect) the logging
		protected StreamWriter m_stream;			// Instance of stream writer for the output log
		protected string m_strApplicationName;		// The name of the calling application
		protected string m_strHostName;				// The name of the host we are logging for

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="fullPath">The full path to the log file.</param>
		public Log(string fullPath)
		{
			// Initialize defaults
			m_strApplicationName = "app";
			m_strHostName = "localhost";

			// Create the synchronization mutex
			m_mutex = new Mutex();

			// Append to the specified file
			m_stream = new StreamWriter(fullPath, true);
		}

		/// <summary>
		/// Add an entry to the log
		/// </summary>
		/// <param name="s">The string to add to the log</param>
		/// <param name="_params">Arguments to a formatted "s" value</param>
		public void Add(string s, params object[] _params)
		{
			// Wait for the mutex to become available
			m_mutex.WaitOne();

			string strEntry;		// Syslog-style log entry

			// Format the log entry
			strEntry = string.Format("{0} {1} {2}: {3}", 
				DateTime.Now.ToString("MMM dd HH:mm:ss"), 
				m_strHostName, 
				m_strApplicationName,
				string.Format(s, _params)
			);

			// Write the formatted string to the log
			m_stream.WriteLine(strEntry);

			// Write any pending text
			m_stream.Flush();

			// Release the mutex
			m_mutex.ReleaseMutex();
		}

        /// <summary>
		/// Add an entry to the log
		/// </summary>
		/// <param name="s">The string to add to the log</param>
		public void Add(string s)
		{
            this.Add("{0}", s);
        }

		/// <summary>
		/// The calling application name
		/// </summary>
		public string applicationName
		{
			get { return m_strApplicationName; }
			set { m_strApplicationName = value; }
		}

		/// <summary>
		/// The calling application host name
		/// </summary>
		public string hostName
		{
			get { return m_strHostName; }
			set { m_strHostName = value; }
		}
	}
}
